﻿using System;
using System.Diagnostics;

namespace PlayTrace
{
    static class TestTraceSource
    {
        private static void CheckPrinting(this TraceSource ts)
        {
            ts.TraceEvent(TraceEventType.Warning, 1, "TestWarning");

            for (int index = 0; index < 4; index++)
            {
                // by using "TraceInformation", "TraceWarning" or "TraceError", event's Id always uses its default, which is zero
                ts.TraceInformation("loop-{0},......", index + 1);
            }

            ts.TraceEvent(TraceEventType.Error, 2, "TestError");
            ts.Close();// Close will call Flush implicitly
        }

        private static void TestManualBuild()
        {
            TraceSource ts = new TraceSource("4test") { Switch = { Level = SourceLevels.Information } };

            ts.Listeners.Clear();
            ts.Listeners.Add(new ConsoleTraceListener());

            TextWriterTraceListener fileListener = new TextWriterTraceListener("myTraceSource-manual.log")
                                                       {
                                                           Filter = new EventTypeFilter(SourceLevels.Warning),
                                                           TraceOutputOptions = TraceOptions.Timestamp | TraceOptions.ThreadId
                                                       };
            ts.Listeners.Add(fileListener);

            ts.CheckPrinting();
        }

        private static void TestFromConfigFile()
        {
            TraceSource ts = new TraceSource("myTraceSource");
            ts.CheckPrinting();
        }

        public static void TestMain()
        {
            // TestManualBuild();
            TestFromConfigFile();
        }
    }
}
